package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.TbProduct;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.ProductService;
import com.thinkgem.jeesite.modules.sys.service.TbAgentProductService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by loutong on 2016/11/6.
 */

@Controller
@RequestMapping(value="${adminPath}/sys/tbAgentProductController")
public class TbAgentProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private TbAgentProductService tbAgentProductService;

    @RequestMapping(value = {"showProductPage", ""})
    public String showProductList(TbProduct tbProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
        User user = UserUtils.getUser();
        tbProduct.setAgentId(user.getId());
        Page<TbProduct> page = productService.findByPage(new Page<TbProduct>(request, response), tbProduct);
        model.addAttribute("page", page);
        if(page.getList().size()==0){
            model.addAttribute("message1","无查询结果");
        }
        return "modules/sys/showProductPage";
    }
    @RequestMapping(value = "updateStatus")
    public String updateStatus(TbProduct tbProduct, RedirectAttributes redirectAttributes) {
        tbAgentProductService.updateStatus(tbProduct);
        addMessage(redirectAttributes, "商品状态修改成功");
        return "redirect:"+ Global.getAdminPath()+"/sys/tbAgentProductController/showProductPage/?repage";
    }
    /**
     * 添加Flash消息
     *
     * @param messages
     */
    protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages) {
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }
        redirectAttributes.addFlashAttribute("message", sb.toString());
    }
}
